//
//  CCNickViewController.m
//  Activity
//
//  Created by coco on 15/5/8.
//  Copyright (c) 2015年 coco. All rights reserved.
//

#import "CCRNickViewController.h"
@interface CCRNickViewController ()
@property (nonatomic, strong) UIImageView *head;
@property (nonatomic, strong) UILabel *nickLabel;
@property (nonatomic, strong) UITextField *nick;
@property (nonatomic, strong) UILabel *passwordLabel;
@property (nonatomic, strong) UITextField *password;
@property (nonatomic, strong) UIButton *next;
@property (nonatomic, strong) CCRNickViewModel *viewModel;

@end

@implementation CCRNickViewController

- (id) initWithViewModel:(CCRNickViewModel *)viewModel {
  if (self = [super init]) {
    _viewModel = viewModel;
  }
  return self;
}

- (void)viewDidLoad {
  [self setupUI];
  [self setupCSS];
  [self bindUI];
  [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void) setupUI {
  _head = [[UIImageView alloc] initWithFrame:CGRectZero];
#warning 测试头像
  _head.backgroundColor = [UIColor redColor];
  _nickLabel = [[UILabel alloc] init];
  _nickLabel.text = @"昵称";
  _nick = [[UITextField alloc] init];
  _nick.layer.cornerRadius = 5.0f;
  _nick.layer.borderWidth = 0.1f;
  _nick.placeholder = @"请输入昵称";
  _passwordLabel = [[UILabel alloc] init];
  _passwordLabel.text = @"密码";
  _password = [[UITextField alloc] init];
  _password.layer.cornerRadius = 5.0f;
  _password.layer.borderWidth = 0.1f;
  _password.placeholder = @"请输入密码";
  _next = [UIButton buttonWithType:UIButtonTypeCustom];
  _next = [UIButton buttonWithType:UIButtonTypeCustom];
  [_next setTitle:@"下一步" forState:UIControlStateNormal];
  [_next setTitleColor:RGB(255, 255, 255, 1) forState:UIControlStateNormal];
  [_next setBackgroundImage:IMAGE(@"btn_normal") forState:UIControlStateNormal];

  [self.view addSubview: _head];
  [self.view addSubview:_nickLabel];
  [self.view addSubview:_nick];
  [self.view addSubview: _passwordLabel];
  [self.view addSubview: _password];
  [self.view addSubview:_next];
}

- (void) setupCSS {
  [_head mas_makeConstraints:^(MASConstraintMaker *make) {
    make.size.mas_equalTo(CGSizeMake(120, 120));
    make.top.mas_equalTo(self.view).offset(20 + self.yoffset);
    make.centerX.equalTo(self.view);
  }];
  
  [_nickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view).offset(20);
    make.top.equalTo(self.head.mas_bottom).offset(20);
  }];
  [_nick mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.nickLabel.mas_right).offset(10);
    make.top.equalTo(self.head.mas_bottom).offset(20);
    make.right.equalTo(self.view).offset(-20);
    make.height.mas_equalTo(40);
  }];
  
  [_passwordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view).offset(20);
    make.top.equalTo(self.nickLabel.mas_bottom).offset(20);
  }];
  [_password mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.passwordLabel.mas_right).offset(10);
    make.top.equalTo(self.nick.mas_bottom).offset(20);
    make.right.equalTo(self.view).offset(-20);
    make.height.mas_equalTo(40);
  }];
  
  [_next mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view).offset(20);
    make.top.equalTo(self.password.mas_bottom).offset(20);
    make.right.equalTo(self.view).offset(-20);
    make.height.mas_equalTo(40);
  }];
}

- (void) bindUI {
  self.next.rac_command = [[RACCommand alloc]
                           initWithEnabled:[RACSignal combineLatest:@[self.nick.rac_textSignal, self.password.rac_textSignal]
                                                             reduce:^id(NSString *nick, NSString *password){
                                                               return @(nick.length > 0 && password.length > 0);
                                                             }]
                           signalBlock:^RACSignal *(id input) {
                             return [_viewModel setNickName:_nick.text password:_password.text headImage:@""];
                                                  }];
}
@end
